package models;

import static com.google.api.services.datastore.client.DatastoreHelper.makeFilter;
import static com.google.api.services.datastore.client.DatastoreHelper.makeOrder;
import static com.google.api.services.datastore.client.DatastoreHelper.makeValue;

import java.util.ArrayList;
import java.util.List;

import com.google.api.services.datastore.DatastoreV1.CommitRequest;
import com.google.api.services.datastore.DatastoreV1.Entity;
import com.google.api.services.datastore.DatastoreV1.EntityResult;
import com.google.api.services.datastore.DatastoreV1.Filter;
import com.google.api.services.datastore.DatastoreV1.Mutation;
import com.google.api.services.datastore.DatastoreV1.Property;
import com.google.api.services.datastore.DatastoreV1.PropertyFilter;
import com.google.api.services.datastore.DatastoreV1.PropertyOrder;
import com.google.api.services.datastore.DatastoreV1.Query;
import com.google.api.services.datastore.DatastoreV1.QueryResultBatch;
import com.google.api.services.datastore.DatastoreV1.RunQueryRequest;
import com.google.api.services.datastore.DatastoreV1.RunQueryResponse;

import entities.IPRange;

public class DS_IPRangeModel {

	public static boolean insertDatastore(IPRange obj) {
		try {
			CommitRequest.Builder creq = CommitRequest.newBuilder();
			creq.setMode(CommitRequest.Mode.NON_TRANSACTIONAL);

			creq.setMutation(Mutation.newBuilder().addInsert(
					DS_Config.ipRangeToDatastoreEntity(obj)));

			DS_Config.getDatastore().commit(creq.build());
		} catch (Exception exception) {
			exception.printStackTrace();
			return false;
		}
		return true;
	}

	public static IPRange checkIPInRange(Double ipValue) {
		try {
			Filter heightMinFilter = makeFilter("min",
					PropertyFilter.Operator.LESS_THAN_OR_EQUAL,
					makeValue(ipValue)).build();

			// Use makeCompositeFilter() to combine multiple filters
			Filter heightRangeFilter = makeFilter(heightMinFilter).build();

			// Use Query.Builder to assemble a query
			Query.Builder q = Query.newBuilder();
			q.addKindBuilder().setName("IPRange");
			q.setLimit(1);
			q.addOrder(makeOrder("min", PropertyOrder.Direction.DESCENDING));
			q.setFilter(heightRangeFilter).build();

			RunQueryRequest.Builder request = RunQueryRequest.newBuilder();
			request.setQuery(q);
			RunQueryResponse response = DS_Config.getDatastore().runQuery(
					request.build());

			if (response.getBatch().getMoreResults() == QueryResultBatch.MoreResultsType.NOT_FINISHED) {
				System.err.println("WARNING: partial results\n");
			}
			List<EntityResult> results = response.getBatch()
					.getEntityResultList();
			List<Entity> entities = new ArrayList<Entity>(results.size());
			for (EntityResult result : results) {
				entities.add(result.getEntity());
			}

			if (entities != null && entities.size() > 0) {
				Entity e = entities.get(0);
				IPRange range = new IPRange();
				for (Property p : e.getPropertyList()) {
					if (p.getName().equals("id")) {
						range.setId(p.getValue().getStringValue());
					} else if (p.getName().equals("max")) {
						range.setMax(p.getValue().getDoubleValue());
					} else if (p.getName().equals("min")) {
						range.setMin(p.getValue().getDoubleValue());
					} else if (p.getName().equals("whois")) {
						range.setWhois(p.getValue().getStringValue());
					}
				}
				if (range.getMax() >= ipValue) {
					return range;
				}
			}
			return null;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;

	}
}